"""
    pygame事件机制学习
"""

import pygame
from pygame.locals import *
from sys import exit

'''
    展示所有的触发的事件的进程
'''
def show_python_event():
    pygame.init()
    screen = pygame.display.set_mode((1080, 720), 0, 32)

    font = pygame.font.SysFont('arial', 16)
    font_height = font.get_linesize()
    event_text = []

    while True:
        event = pygame.event.wait()
        event_text.append(str(event))
        # 将屏幕高度除以行高，作为event_text逆向切片的起始值
        # 这样可以保证只展示最后一个屏幕的内容
        event_text = event_text[-int(720 / font_height):]

        if event.type == QUIT:
            exit()

        # 填充屏幕背景为黑色
        screen.fill((0, 0, 0))

        # 将内容从下至上展示在屏幕上
        y = 720 - font_height
        for text in reversed(event_text):
            # 屏幕展示字的内容 颜色以及位置
            screen.blit(font.render(text, True, (0, 255, 0)), (0, y))
            # 向上移动一行
            y -= font_height

        pygame.display.update()


'''
    键盘事件展示方法
'''
def show_keyboard_event():
    pygame.init()
    screen = pygame.display.set_mode((640, 480), 0, 32)
    background = pygame.image.load('../img/game1-helloworld/mouse_80x80_alpha.png').convert()

    x, y = 0, 0
    move_x, move_y = 0, 0

    while True:
        for event in pygame.event.get():
            if event.type == QUIT:
                exit()
            if event.type == KEYDOWN:
                if event.key == K_LEFT:
                    move_x = -1
                elif event.key == K_RIGHT:
                    move_x = 1
                elif event.key == K_UP:
                    move_y = -1
                elif event.key == K_DOWN:
                    move_y = 1
            elif event.type == KEYUP:
                move_x, move_y = 0, 0

        x += move_x
        y += move_y

        screen.fill((0, 0, 0))
        screen.blit(background, (x, y))
        pygame.display.update()


'''
    主进程
'''
if __name__ == '__main__':
    show_python_event()
